<?php
declare(strict_types=1);
namespace app\shop\logic;
use app\admin\model\play\Setting;
use app\shop\exception\FailException;
use app\shop\model\play\user;
use think\Collection;
use think\facade\Db;
use think\response\Json;

class PlayLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new user();
    }

    public function PlayList($params): Collection|array
    {
        $query = user::getQuery();
        $settingmodel = new Setting();
        $where = [];
        if(isset($params['keyword']) && !empty($params['keyword'])){
            if($params['keyword'] == "男"){
                $play_id = Db::name('play_user')->where('sex',1)->column('id');
                $where[] = ['play_user_id','in',$play_id];
            }elseif ($params['keyword'] == '女'){
                $play_id = Db::name('play_user')->where('sex',2)->column('id');
                $where[] = ['play_user_id','in',$play_id];
            }else{
                $user_id = Db::name('shop_user')->where('nickname','like','%'.$params['keyword'].'%')->column('id');
                $where[] = ['user_id','in',$user_id];
            }
        }
        $playuserids = $settingmodel->where($where)->where('switch',1)->column('play_user_id');
        $query->where([
            ['id', 'in', $playuserids],
        ]);
        $this->buildEqualQuery($query, $params, [
        ]);

        $query->field([
            '*'
        ]);
        $query->order(['id' => 'desc']);
        $return = user::listQuery($query);
        if($return){
            foreach ($return as $key => $item) {
                $return[$key]['username'] = Db::name('shop_user')->where('id', $item['shop_user_id'])->value('nickname');
                $return[$key]['avatar'] = Db::name('shop_user')->where('id', $item['shop_user_id'])->value('avatar');
            }
        }
        return $return;
    }

    public function detail(array $params): array
    {

        $query = user::where([
            ['shop_user_id', '=', $params['user_id']],
        ]);
        $result = $query
            ->findOrEmpty();

        if ($result->isEmpty()) {
            return [];
        }
        $settingmodel = new Setting();
        $result->username = Db::name('shop_user')->where('id', $result->shop_user_id)->value('nickname');
        $result->avatar = Db::name('shop_user')->where('id', $result->shop_user_id)->value('avatar');
        $result->setting = $settingmodel->where('play_user_id', $result->id)->find();
        return $result->toArray();
    }

}